package com.woniu.cms.psm.core.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.woniu.cms.psm.core.entity.PmFeasibility;
import com.woniu.cms.psm.core.service.IPmFeasibilityService;
import com.woniu.cms.psm.core.vo.FeasibilityVo;
import com.woniu.cms.psm.core.vo.FeasibilityWithAttachmentVo;
import com.woniu.cms.psm.core.vo.PageVo;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lilili
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/pm-feasibility")
@Api(tags = "项目立项控制器")
public class PmFeasibilityController {
    @Resource
    private IPmFeasibilityService feasibilityService;

    /**
     * 获取分页数据
     * 年份用year函数获得，只取前四位
     *
     * @param pageNo   当前页数
     * @param pageSize 每页大小
     * @return {@link Result}<{@link PageVo}<{@link FeasibilityVo}>>
     */
    @ApiOperation("获取立项表分页数据")
    @GetMapping("/page/{pageNo}/{pageSize}")
    public Result<PageVo<FeasibilityVo>> getPage(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize){
        PageVo<FeasibilityVo> pageVo = feasibilityService.getPage(pageNo, pageSize);
        return Result.common(pageVo);
    }

    /**
     * 根据id批量删除
     *
     * @param ids id
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation("批量删除立项表单")
    @DeleteMapping("/delete")
    public Result<Object> delete(@RequestBody Integer[] ids){
        boolean delete = feasibilityService.delete(ids);
        return Result.common(delete);
    }

    /**
     * 添加或更新项目表
     *  获取一个立项项目表对象即可
     *  但是有些需要调用微服务
     *  1.项目经理   指定职位的用户名称
     *  2.技术负责人     指定职位的用户名称
     *  3.建设单位管理员：客户联系人资料
     *  4.建设单位管理部门，和建设单位管理员一起引入
     *  5.劳务合同单位，劳务合同单位字典
     *  6.合同编号，关联合同
     *
     *
     *  以及提交附件
     *  1.（提交文档） 增删查
     *
     *
     *
     * @param feasibility 可行性
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation("添加项目立项表")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody PmFeasibility feasibility, @RequestParam List<Integer> ids){
        boolean b = feasibilityService.add(feasibility, ids);
        if(b){
            return Result.common(b);
        }else{
            return Result.error(ResultCode.FAILED);
        }
    }

    /**
     * 查看项目立项表，用于数据回显
     *
     * @param id id
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation("查看项目立项表")
    @GetMapping("/look/{id}")
    public Result<Object> look(@PathVariable("id") Integer id){
        FeasibilityWithAttachmentVo vo = feasibilityService.look(id);


        if(vo == null){
            return Result.error(ResultCode.FAILED);
        }else{
            return Result.common(vo);
        }
    }


    /**
     * 项目立项审批
     *
     * @param id id
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation("项目立项审批")
    @PutMapping("/auth/check/{id}")
    public Result<Object> check(@PathVariable("id") Integer id, HttpServletRequest request){

        String userName = request.getHeader("userName");
        boolean check = feasibilityService.check(id, userName);
        return Result.common(check);

    }
}

